<?php
namespace app\index\controller;
use app\admin\model\ProfessClassify;
use app\admin\model\ProfessLunbo;
use app\admin\model\ProfessAdvert;
use app\admin\model\ProfessForum;
use app\admin\model\ProfessForumReply;
use app\admin\model\MemberUser;
use app\admin\model\ProfessForumClick;
use app\admin\model\ProfessForumReplyClick;
use think\Controller;

class Profess extends Controller
{
    //朋友圈首页
    public function index()
    {
        $data=array();

        //查询轮播
        $lunboRes=ProfessLunbo::order('lun_sort desc')->select();

        //查询广告
        $advertRes=ProfessAdvert::order('v_sort desc')->select();

        //查询分类
        $classify= ProfessClassify::select();

        $data['lunboRes']=$lunboRes;
        $data['advertRes']=$advertRes;
        $data['classify']=$classify;
        return json(array('code'=>1,'data'=>$data));
    }
    //查询首页话题
    public function indexForum(){
        $post=input('post.');
        $page=$post['pageid'];
        //最新发布
        if($post['findType']==1){
            $data= ProfessForum::page($page,10)->order('pf_createtime desc')->select();
        }
        //距离最近
        elseif($post['findType']==2){
            $longitude=$post['pf_x_coordinate'];
            $latitude=$post['pf_y_coordinate'];
            $forumRes= ProfessForum::select();
            foreach($forumRes as $k=>$v){
                $forumRes[$k]['juli'] = $this->getDistance($longitude, $latitude, $v['pf_x_coordinate'], $v['pf_y_coordinate'], 2);
            }
            if(count($forumRes)>=1){
                $data=$forumRes;
            }else{
                $teturnArray = $this->arraySort($forumRes, 'juli', SORT_DESC);
                $data=array_slice($teturnArray, ($page-10)*10,10);
            }
        }
        //最热回复
        elseif($post['findType']==3){
            $forumRes= ProfessForum::select();
            foreach($forumRes as $k=>$v){
                $whereAA['pf_id']=$v['id'];
                $forumRes[$k]['zonghot']=ProfessForumReply::where($whereAA)->count();
            }
            if(count($forumRes)>=1){
                $data=$forumRes;
            }else{
                $teturnArray = $this->arraySort($forumRes, 'zonghot', SORT_DESC);
                $data=array_slice($teturnArray, ($page-10)*10,10);
            }
        }
        //最热话题
        else{
            $data= ProfessForum::page($page,10)->order('pf_clickzan_number desc')->select();
        }

        foreach($data as $k=>$v){
            $data[$k]['pf_createtime']=date('Y-m-d H:i',$v['pf_createtime']);

            $data[$k]['pf_image'] = explode(',',$v['pf_image']);

            $whereAP['pf_id']=$v['id'];
            $data[$k]['zonghot']=ProfessForumReply::where($whereAP)->count();

            //查询用户数据
            $whereAB['id']=$v['user_id'];
            $data[$k]['user']=MemberUser::where($whereAB)->find();
        }
        return json(array('code'=>1,'data'=>$data));
    }
    //按照经纬度坐标计算距离
    public function getDistance($longitude1, $latitude1, $longitude2, $latitude2, $unit=2, $decimal=2){

        $EARTH_RADIUS = 6370.996; // 地球半径系数
        $PI = 3.1415926;

        $radLat1 = $latitude1 * $PI / 180.0;
        $radLat2 = $latitude2 * $PI / 180.0;

        $radLng1 = $longitude1 * $PI / 180.0;
        $radLng2 = $longitude2 * $PI /180.0;

        $a = $radLat1 - $radLat2;
        $b = $radLng1 - $radLng2;

        $distance = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2)));
        $distance = $distance * $EARTH_RADIUS * 1000;

        if($unit==2){
            $distance = $distance / 1000;
        }
        return round($distance, $decimal);
    }
    public function arraySort($arr,$shortKey,$short=SORT_DESC,$shortType=SORT_REGULAR){
        foreach ($arr as $key => $data){
            $name[$key] = $data[$shortKey];
        }
        array_multisort($name,$shortType,$short,$arr);
        return $arr;
    }
    //查询文章分类
    public function forumClassify(){
        $data= ProfessClassify::select();
        return json(array('code'=>1,'data'=>$data));
    }
    //发布信息
    public function forumSumbit(){
        $post=input('post.');
        $data=[
            'user_id'=>$post['user_id'],
            'cl_id'=>$post['cl_id'],
            'pf_title'=>$post['pf_title'],
            'pf_image'=>implode(',',$post['pf_image']),
            'pf_content'=>$post['pf_content'],
            'pf_province'=>$post['pf_province'],
            'pf_city'=>$post['pf_city'],
            'pf_area'=>$post['pf_area'],
            'pf_address'=>$post['pf_address'],
            'pf_phone'=>$post['pf_phone'],
            'pf_x_coordinate'=>$post['pf_x_coordinate'],
            'pf_y_coordinate'=>$post['pf_y_coordinate'],
            'pf_createtime'=>time()
        ];
        $save=ProfessForum::insert($data);
        if($save){
            return json(array('code'=>1,'msg'=>'提交成功！'));
        }
        return json(array('code'=>0,'msg'=>'提交失败！'));
    }
    //查询分类详情
    public function classifyDetail(){
        $post=input('post.');

        //查询分类数据
        $whereAA['cl_id']=$post['cl_id'];
        $classify=ProfessClassify::where($whereAA)->find();

        //查询话题个数
        $forumCount=ProfessForum::where($whereAA)->count();

        //查询回复个数
        $forumReplyCount=ProfessForumReply::where($whereAA)->count();
        $data=[
            'classify'=>$classify,
            'forumCount'=>$forumCount,
            'forumReplyCount'=>$forumReplyCount,
        ];
        return json(array('code'=>1,'data'=>$data));
    }
    //文章列表
    public function forumList(){
        $post=input('post.');
        $page=$post['pageid'];
        $whereAA['cl_id']=$post['cl_id'];
        $data= ProfessForum::where($whereAA)->page($page,10)->select();
        if($data){
            foreach($data as $k=>$v){
                $data[$k]['pf_image'] = explode(',',$v['pf_image']);

                $data[$k]['pf_createtime']=date('Y-m-d H:i',$v['pf_createtime']);

                //查询回复个数
                $whereAA['pf_id']=$v['id'];
                $data[$k]['forumReplyCount']=ProfessForumReply::where($whereAA)->count();

                //查询用户数据
                $whereAB['id']=$v['user_id'];
                $data[$k]['user']=MemberUser::where($whereAB)->find();
            }
            return json(array('code'=>1,'msg'=>'数据返回成功！','data'=>$data));
        }
        return json(array('code'=>0,'msg'=>'暂无数据！'));
    }
    //文章详情
    public function forumDetail(){
        $post=input('post.');
        $whereAA['id']=$post['id'];
        $data=ProfessForum::where($whereAA)->find();
        if($data){
            $data['pf_image'] = explode(',',$data['pf_image']);
            $dataAA['pf_clicknumber']=$data['pf_clicknumber']+1;
            ProfessForum::where($whereAA)->update($dataAA);

            $data['pf_createtime']=date('Y-m-d H:i',$data['pf_createtime']);

            //查询用户数据
            $whereAB['id']=$data['user_id'];
            $data['user']=MemberUser::where($whereAB)->find();

            $whereAD['user_id']=$data['user_id'];
            $whereAD['pf_id']=$data['id'];
            $data['userClickCon']=ProfessForumClick::where($whereAD)->count();

            //查询评论数量
            $whereAC['pf_id']=$post['id'];
            $data['replyCount']=ProfessForumReply::count();

            //评论点赞数量
            $data['replyHot']=ProfessForumReply::where($whereAC)->count();
            return json(array('code'=>1,'msg'=>'数据返回成功！','data'=>$data));
        }
        return json(array('code'=>0,'msg'=>'暂无数据！'));
    }
    //文章评论
    public function forumReply(){
        $post=input('post.');
        $whereAA['pf_id']=$post['id'];
        $whereAA['fid']=0;
        $page=$post['pageid'];
        $data=ProfessForumReply::where($whereAA)->page($page,10)->select();
        if($data){
            foreach($data as $k=>$v){
                $whereAD['user_id']=$post['user_id'];
                $whereAD['pfr_id']=$v['bfr_id'];
                $data[$k]['userClickCon']=ProfessForumReplyClick::where($whereAD)->count();


                $whereAB['id']=$v['user_id'];
                $data[$k]['user']=MemberUser::where($whereAB)->find();

                $whereAC['fid']=$v['bfr_id'];
                $data[$k]['reply']=ProfessForumReply::where($whereAC)->select();
                foreach($data[$k]['reply'] as $key=>$val){
                    $whereADC['id']=$val['user_id'];
                    $data[$k]['reply'][$key]['user']=MemberUser::where($whereADC)->find();


                    $whereADd['user_id']=$post['user_id'];
                    $whereADd['pfr_id']=$val['bfr_id'];
                    $data[$k]['reply'][$key]['userClickCon']=ProfessForumReplyClick::where($whereADd)->count();
                    //return json(array($data));
                }
            }
            return json(array('code'=>1,'msg'=>'数据返回成功！','data'=>$data));
        }
        return json(array('code'=>0,'msg'=>'暂无数据！'));
    }
    //给文章点赞
    public function clickZan(){
        $post=input('post.');
        $whereAA['id']=$post['id'];
        $data=ProfessForum::where($whereAA)->find();
        if($data){
            $whereAB['id']=$post['user_id'];
            $user=MemberUser::where($whereAB)->find();
            if($user){
                //判断用户是否点过赞
                $whereAC['user_id']=$post['user_id'];
                $whereAC['pf_id']=$post['id'];

                $click=ProfessForumClick::where($whereAC)->find();

                if($click){
                    $dataAA['pf_clickzan_number']=$data['pf_clickzan_number']-1;
                    ProfessForum::where($whereAA)->update($dataAA);
                    $save=ProfessForumClick::where($whereAC)->delete();
                }else{
                    $dataAA['pf_clickzan_number']=$data['pf_clickzan_number']+1;
                    ProfessForum::where($whereAA)->update($dataAA);

                    $dataSV['pf_id']=$post['id'];
                    $dataSV['user_id']=$post['user_id'];
                    $save=ProfessForumClick::insert($dataSV);
                }
                if($save){
                    return json(array('code'=>1,'msg'=>'操作成功！'));
                }
                return json(array('code'=>0,'msg'=>'操作失败！'));
            }
            return json(array('code'=>0,'msg'=>'用户不存在！'));
        }
        return json(array('code'=>0,'msg'=>'文章不存在！'));
    }
    //给评论点赞
    public function replyClickZan(){
        $post=input('post.');
        $whereAA['bfr_id']=$post['pfr_id'];
        $data=ProfessForumReply::where($whereAA)->find();
        if($data){
            $whereAB['id']=$post['user_id'];
            $user=MemberUser::where($whereAB)->find();
            if($user){
                //判断用户是否点过赞
                $whereAC['user_id']=$post['user_id'];
                $whereAC['pfr_id']=$post['pfr_id'];

                $click=ProfessForumReplyClick::where($whereAC)->find();

                if($click){
                    $save=ProfessForumReplyClick::where($whereAC)->delete();
                }else{
                    $dataSV['pfr_id']=$post['pfr_id'];
                    $dataSV['user_id']=$post['user_id'];
                    $dataSV['pf_id']=$data['pf_id'];
                    $save=ProfessForumReplyClick::insert($dataSV);
                }
                if($save){
                    return json(array('code'=>1,'msg'=>'操作成功！'));
                }
                return json(array('code'=>0,'msg'=>'操作失败！'));
            }
            return json(array('code'=>0,'msg'=>'用户不存在！'));
        }
        return json(array('code'=>0,'msg'=>'评论不存在！'));
    }
    //文章评论
    public function forumComment(){
        $post=input('post.');
        $whereAA['id']=$post['id'];
        $forum=ProfessForum::where($whereAA)->find();
        if($forum){
            $data=[
                'user_id'=>$post['user_id'],
                'pf_id'=>$post['id'],
                'cl_id'=>$forum['cl_id'],
                'fid'=>$post['fid'],
                'bfr_title'=>$post['bfr_title'],
                'bfr_createtime'=>time()
            ];
            $save=ProfessForumReply::insert($data);
            if($save){
                return json(array('code'=>1,'msg'=>'提交成功！'));
            }
            return json(array('code'=>0,'msg'=>'评论失败！'));
        }
        return json(array('code'=>0,'msg'=>'文章不存在！'));
    }
    public function shopImage(){
        $file =request()->file('file');
        $ret = $this->upload($file);
        return json($ret);
    }





}
